﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace AutocompleteDemo
{
    [System.Runtime.InteropServices.Guid("53C0AC5E-A58A-43E9-8389-4733BEF5277A")]
    public sealed class AutoCompletionServiceApplicationProxy : SPIisWebServiceApplicationProxy
	{
        AutoCompletionServiceApplication m_application;

        // An empty public constructor required for serialization.
        public AutoCompletionServiceApplicationProxy() 
        {
        }

        public AutoCompletionServiceApplicationProxy(string name, SPIisWebServiceProxy serviceProxy, Uri serviceEndpointUri)
            : base(name, serviceProxy, serviceEndpointUri)
        {

        }

        internal AutoCompletionServiceApplicationProxy(string name, SPIisWebServiceProxy serviceProxy, AutoCompletionServiceApplication serviceApplication)
            : base(name, serviceProxy, serviceApplication.Uri)
        {
            this.m_application = serviceApplication;
        }

        public AutoCompletionServiceApplication AutoCompletionApplication
        {
            get
            {
                AutoCompletionServiceApplication app = m_application;
                if (app == null)
                {
                    app = AutoCompletionServiceApplication.GetLocalApplicationByName(this.Name);
                    if (app == null)
                        throw new ApplicationException("Auto Completion service application not available.");
                }

                return app;
            }
        }

        public static AutoCompletionServiceApplicationProxy GetProxy(SPServiceContext serviceContext)
        {
            if (serviceContext == null)
            {
                throw new ArgumentNullException("serviceContext");
            }
            return (serviceContext.GetDefaultProxy(typeof(AutoCompletionServiceApplicationProxy)) as AutoCompletionServiceApplicationProxy);
        }
	}
}
